package com.itheima.algorithm.leetcode;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;

/**
 * Leetcode 很多链表题目用到的节点类
 */
public class ListNode {
    public int val;
    public ListNode next;
    ListNode() {

    }
    public ListNode(int val) {
        this.val = val;
    }
    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }

    public static ListNode of(Integer... val){
        if (val.length == 0) {
            return null;
        }
        ListNode p = null;
        for (int i = val.length - 1; i >= 0; i--) {
            p = new ListNode(val[i], p);
        }
        return p;
    }

    // 有环的情况下会无限循环
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder(64);
        sb.append("[");
        ListNode p = this;
        while (p != null) {
            sb.append(p.val);
            if (p.next != null) {
                sb.append(",");
            }
            p = p.next;
        }
        sb.append("]");
        return sb.toString();
    }

//    @Override
//    public String toString() {
//        StringBuilder sb = new StringBuilder(64);
//        sb.append("[");
//        sb.append(this.val);
//        sb.append("]");
//        return sb.toString();
//    }
}